from PyQt5 import QtCore, QtGui, QtWidgets
import sqlite3
import sys
from PyQt5.QtWidgets import QApplication,QWidget

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(566, 489)
        Form.setFocusPolicy(QtCore.Qt.StrongFocus)
        Form.setAutoFillBackground(True)
        self.label_13 = QtWidgets.QLabel(Form)
        self.label_13.setGeometry(QtCore.QRect(220, 20, 161, 31))
        self.label_13.setObjectName("label_13")
        self.label_7 = QtWidgets.QLabel(Form)
        self.label_7.setGeometry(QtCore.QRect(320, 240, 59, 31))
        self.label_7.setObjectName("label_7")
        self.label_6 = QtWidgets.QLabel(Form)
        self.label_6.setGeometry(QtCore.QRect(320, 70, 59, 61))
        self.label_6.setObjectName("label_6")
        self.horizontalLayoutWidget = QtWidgets.QWidget(Form)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(170, 420, 221, 51))
        self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.pushButton_save = QtWidgets.QPushButton(self.horizontalLayoutWidget)
        font = QtGui.QFont()
        font.setFamily("宋体")
        font.setPointSize(12)
        font.setBold(False)
        font.setWeight(50)
        self.pushButton_save.setFont(font)
        self.pushButton_save.setObjectName("pushButton_save")
        self.horizontalLayout.addWidget(self.pushButton_save)
        spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout.addItem(spacerItem)
        self.pushButton_cancel = QtWidgets.QPushButton(self.horizontalLayoutWidget)
        font = QtGui.QFont()
        font.setFamily("宋体")
        font.setPointSize(12)
        font.setBold(False)
        font.setWeight(50)
        self.pushButton_cancel.setFont(font)
        self.pushButton_cancel.setObjectName("pushButton_cancel")
        self.horizontalLayout.addWidget(self.pushButton_cancel)
        self.label_8 = QtWidgets.QLabel(Form)
        self.label_8.setGeometry(QtCore.QRect(40, 320, 59, 61))
        self.label_8.setObjectName("label_8")
        self.comboBox_method = QtWidgets.QComboBox(Form)
        self.comboBox_method.setGeometry(QtCore.QRect(100, 220, 161, 22))
        self.comboBox_method.setObjectName("comboBox_method")
        self.comboBox_method.addItem("")
        self.comboBox_method.addItem("")
        self.label = QtWidgets.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(50, 50, 41, 31))
        self.label.setObjectName("label")
        self.lineEdit_id = QtWidgets.QLineEdit(Form)
        self.lineEdit_id.setGeometry(QtCore.QRect(100, 50, 81, 31))
        self.lineEdit_id.setObjectName("lineEdit_id")
        self.label_2 = QtWidgets.QLabel(Form)
        self.label_2.setGeometry(QtCore.QRect(40, 90, 51, 31))
        self.label_2.setObjectName("label_2")
        self.lineEdit_name = QtWidgets.QLineEdit(Form)
        self.lineEdit_name.setGeometry(QtCore.QRect(100, 90, 181, 31))
        self.lineEdit_name.setObjectName("lineEdit_name")
        self.label_3 = QtWidgets.QLabel(Form)
        self.label_3.setGeometry(QtCore.QRect(20, 120, 72, 31))
        self.label_3.setObjectName("label_3")
        self.label_4 = QtWidgets.QLabel(Form)
        self.label_4.setGeometry(QtCore.QRect(40, 220, 51, 31))
        self.label_4.setObjectName("label_4")
        self.label_5 = QtWidgets.QLabel(Form)
        self.label_5.setGeometry(QtCore.QRect(40, 260, 51, 41))
        self.label_5.setObjectName("label_5")
        self.dateEdit_handlingTime = QtWidgets.QDateEdit(Form)
        self.dateEdit_handlingTime.setGeometry(QtCore.QRect(100, 270, 110, 22))
        self.dateEdit_handlingTime.setDateTime(QtCore.QDateTime(QtCore.QDate(2018, 1, 1), QtCore.QTime(0, 0, 0)))
        self.dateEdit_handlingTime.setObjectName("dateEdit_handlingTime")
        self.lineEdit_staff = QtWidgets.QLineEdit(Form)
        self.lineEdit_staff.setGeometry(QtCore.QRect(390, 90, 81, 31))
        self.lineEdit_staff.setText("")
        self.lineEdit_staff.setObjectName("lineEdit_staff")
        self.label_9 = QtWidgets.QLabel(Form)
        self.label_9.setGeometry(QtCore.QRect(320, 130, 59, 61))
        self.label_9.setObjectName("label_9")
        self.doubleSpinBox_charge = QtWidgets.QDoubleSpinBox(Form)
        self.doubleSpinBox_charge.setGeometry(QtCore.QRect(390, 150, 111, 21))
        self.doubleSpinBox_charge.setObjectName("doubleSpinBox_charge")
        self.comboBox_classify = QtWidgets.QComboBox(Form)
        self.comboBox_classify.setGeometry(QtCore.QRect(390, 200, 91, 22))
        self.comboBox_classify.setObjectName("comboBox_classify")
        self.comboBox_classify.addItem("")
        self.comboBox_classify.addItem("")
        self.comboBox_classify.addItem("")
        self.comboBox_classify.addItem("")
        self.label_10 = QtWidgets.QLabel(Form)
        self.label_10.setGeometry(QtCore.QRect(320, 200, 51, 31))
        self.label_10.setObjectName("label_10")
        self.line = QtWidgets.QFrame(Form)
        self.line.setGeometry(QtCore.QRect(290, 90, 20, 311))
        self.line.setFrameShape(QtWidgets.QFrame.VLine)
        self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line.setObjectName("line")
        self.plainTextEdit_problem = QtWidgets.QPlainTextEdit(Form)
        self.plainTextEdit_problem.setGeometry(QtCore.QRect(100, 140, 181, 71))
        self.plainTextEdit_problem.setObjectName("plainTextEdit_problem")
        self.plainTextEdit_problem_2 = QtWidgets.QPlainTextEdit(Form)
        self.plainTextEdit_problem_2.setGeometry(QtCore.QRect(100, 310, 181, 91))
        self.plainTextEdit_problem_2.setObjectName("plainTextEdit_problem_2")
        self.plainTextEdit_remark = QtWidgets.QPlainTextEdit(Form)
        self.plainTextEdit_remark.setGeometry(QtCore.QRect(320, 270, 211, 131))
        self.plainTextEdit_remark.setObjectName("plainTextEdit_remark")

        self.retranslateUi(Form)
        self.pushButton_save.clicked.connect(Form.saveToSqlite)
        self.pushButton_cancel.clicked.connect(Form.close)
        QtCore.QMetaObject.connectSlotsByName(Form)
    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.label_13.setText(_translate("Form", "<html><head/><body><p align=\"center\"><span style=\" font-size:16pt; font-weight:600; color:#0f3978;\">新增售后信息</span></p></body></html>"))
        self.label_7.setText(_translate("Form", "备注"))
        self.label_6.setText(_translate("Form", "处理人员"))
        self.pushButton_save.setText(_translate("Form", "保存"))
        self.pushButton_cancel.setText(_translate("Form", "取消"))
        self.label_8.setText(_translate("Form", "解决方法"))
        self.comboBox_method.setItemText(0, _translate("Form", "远程处理（电话/微信）"))
        self.comboBox_method.setItemText(1, _translate("Form", "上门服务"))
        self.label.setText(_translate("Form", "售后ID"))
        self.lineEdit_id.setText(_translate("Form", "20180001"))
        self.label_2.setText(_translate("Form", "客户名称"))
        self.label_3.setText(_translate("Form", "客户反馈问题"))
        self.label_4.setText(_translate("Form", "处理方式"))
        self.label_5.setText(_translate("Form", "处理时间"))
        self.label_9.setText(_translate("Form", "收费金额"))
        self.comboBox_classify.setItemText(0, _translate("Form", "装配问题"))
        self.comboBox_classify.setItemText(1, _translate("Form", "零件质量问题"))
        self.comboBox_classify.setItemText(2, _translate("Form", "产品设计问题"))
        self.comboBox_classify.setItemText(3, _translate("Form", "使用不当"))
        self.label_10.setText(_translate("Form", "问题归类"))

    def saveToSqlite(self):
        id=int(self.lineEdit_id.text())
        name=str(self.lineEdit_name.text())
        nick1=str(self.lineEdit_nick1.text())
        nick2=str(self.lineEdit_nick2.text())
        nick3=str(self.lineEdit_nick3.text())
        contact=self.textBrowser_contact.toPlainText()
        remark=self.textBrowser_remark.toPlainText()
        print("start")
        content=[id,name,nick1,nick2,nick3,contact,remark]

### 以下开始连接数据库
        conn = sqlite3.connect('../nextai.db')
        curs = conn.cursor()  #创建游标
        print ("打开数据库成功")
        print(id,name,nick1,nick2,nick3,contact,remark)

        cur = conn.execute("SELECT customerID, customer, nickname1, nickname2,nickname3,contact,remark  from customer")
        for row in cur:
           print ("客户ID = ", row[0])
           print ("客户名称 = ", row[1])
           print ("别称1 = ", row[2])
           print ("别称2 = ", row[3])
           print ("别称3 = ", row[4])
           print ("联系方式 = ", row[5])
           print ("备注 = ", row[6], "\n")

#        curs.execute('insert into crm(customerID, customer, nickname1, nickname2,nickname3,contact,remark ) values ("id","name","nick1","nick2","nick3","contact","remark")');

        curs.execute("insert into customer(customerID, customer, nickname1, nickname2,nickname3,contact,remark ) values (?,?,?,?,?,?,?)",content)

        curs.close()  #关闭游标
        conn.commit() #保存数据库
        conn.close() #关闭数据库连接

### 以下分别处理5个【编辑】按钮的响应事件，这个方法太笨了，需要重新写
    def handle_click(self):
        self.show()


class MyForm ( QWidget, Ui_Form ):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
     #   self.lineEdit.textChanged.connect(self.textBrowser.setText)

if __name__=='__main__':
    app=QApplication(sys.argv)
    w = MyForm()
    w.show()
    app.exec_()